package com.openbravo.pos.panels;

import com.openbravo.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.SerializerWrite;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.AppView;
import com.openbravo.pos.forms.DataLogicSales;
import com.openbravo.pos.reports.ReportEditorCreator;
import com.openbravo.pos.ticket.ProductInfoExt;

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;

import javax.accessibility.AccessibleContext;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.EventListenerList;

public class AuxiliarFilter extends JPanel
  implements ReportEditorCreator
{
  private ProductInfoExt product;
  private DataLogicSales m_dlSales;
  protected EventListenerList listeners = new EventListenerList();
  private JButton Enter1;
  private JButton Enter2;
  private JLabel jLabel6;
  private JLabel jLabel7;
  private JTextField m_jBarcode1;
  private JTextField m_jReference1;
  private JTextField m_jSearch;
  private JButton search;

  public AuxiliarFilter()
  {
    initComponents();
  }

  public void init(AppView paramAppView)
  {
    this.m_dlSales = ((DataLogicSales)paramAppView.getBean("com.openbravo.pos.forms.DataLogicSales"));
  }

  public void activate()
    throws BasicException
  {
    this.product = null;
    this.m_jSearch.setText(null);
    this.m_jBarcode1.setText(null);
    this.m_jReference1.setText(null);
  }

  public SerializerWrite getSerializerWrite()
  {
    return SerializerWriteString.INSTANCE;
  }

  public void addActionListener(ActionListener paramActionListener)
  {
    this.listeners.add(ActionListener.class, paramActionListener);
  }

  public void removeActionListener(ActionListener paramActionListener)
  {
    this.listeners.remove(ActionListener.class, paramActionListener);
  }

  public Component getComponent()
  {
    return this;
  }

  public Object createValue()
    throws BasicException
  {
    return this.product == null ? null : this.product.getID();
  }

  public ProductInfoExt getProductInfoExt()
  {
    return this.product;
  }

  private void assignProduct(ProductInfoExt paramProductInfoExt)
  {
    this.product = paramProductInfoExt;
    if (this.product == null)
    {
      this.m_jSearch.setText(null);
      this.m_jBarcode1.setText(null);
      this.m_jReference1.setText(null);
    }
    else
    {
      this.m_jSearch.setText(this.product.getReference() + " - " + this.product.getName());
      this.m_jBarcode1.setText(this.product.getCode());
      this.m_jReference1.setText(this.product.getReference());
    }
    fireSelectedProduct();
  }

  protected void fireSelectedProduct()
  {
    EventListener[] arrayOfEventListener = this.listeners.getListeners(ActionListener.class);
    ActionEvent localActionEvent = null;
    for (int i = 0; i < arrayOfEventListener.length; i++)
    {
      if (localActionEvent == null)
        localActionEvent = new ActionEvent(this, 1001, "SELECTED");
      ((ActionListener)arrayOfEventListener[i]).actionPerformed(localActionEvent);
    }
  }

  private void assignProductByCode()
  {
    try
    {
      ProductInfoExt localProductInfoExt = this.m_dlSales.getProductInfoByCode(this.m_jBarcode1.getText());
      if (localProductInfoExt == null)
        Toolkit.getDefaultToolkit().beep();
      assignProduct(localProductInfoExt);
    }
    catch (BasicException localBasicException)
    {
      MessageInf localMessageInf = new MessageInf(localBasicException);
      localMessageInf.show(this);
      assignProduct(null);
    }
  }

  private void assignProductByReference()
  {
    try
    {
      ProductInfoExt localProductInfoExt = this.m_dlSales.getProductInfoByReference(this.m_jReference1.getText());
      if (localProductInfoExt == null)
        Toolkit.getDefaultToolkit().beep();
      assignProduct(localProductInfoExt);
    }
    catch (BasicException localBasicException)
    {
      MessageInf localMessageInf = new MessageInf(localBasicException);
      localMessageInf.show(this);
      assignProduct(null);
    }
  }

  private void initComponents()
  {
    this.jLabel6 = new JLabel();
    this.m_jReference1 = new JTextField();
    this.Enter1 = new JButton();
    this.jLabel7 = new JLabel();
    this.m_jBarcode1 = new JTextField();
    this.Enter2 = new JButton();
    this.m_jSearch = new JTextField();
    this.search = new JButton();
    setBorder(BorderFactory.createTitledBorder(AppLocal.getIntString("label.byproduct")));
    this.jLabel6.setText(AppLocal.getIntString("label.prodref"));
    this.m_jReference1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        AuxiliarFilter.this.m_jReference1ActionPerformed(paramActionEvent);
      }
    });
    this.Enter1.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/apply.png")));
    this.Enter1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        AuxiliarFilter.this.Enter1ActionPerformed(paramActionEvent);
      }
    });
    this.jLabel7.setText(AppLocal.getIntString("label.prodbarcode"));
    this.m_jBarcode1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        AuxiliarFilter.this.m_jBarcode1ActionPerformed(paramActionEvent);
      }
    });
    this.Enter2.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/apply.png")));
    this.Enter2.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        AuxiliarFilter.this.Enter2ActionPerformed(paramActionEvent);
      }
    });
    this.m_jSearch.setEditable(false);
    this.m_jSearch.setFocusable(false);
    this.m_jSearch.setRequestFocusEnabled(false);
    this.search.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/search.png")));
    this.search.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        AuxiliarFilter.this.searchActionPerformed(paramActionEvent);
      }
    });
    GroupLayout localGroupLayout = new GroupLayout(this);
    setLayout(localGroupLayout);
    localGroupLayout.setHorizontalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel6, -2, 125, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.m_jReference1, -2, 241, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.Enter1)).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel7, -2, 125, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.m_jBarcode1, -2, 241, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.Enter2)).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.m_jSearch, -2, 378, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.search))).addContainerGap(-1, 32767)));
    localGroupLayout.setVerticalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel6).addComponent(this.m_jReference1, -2, -1, -2)).addComponent(this.Enter1)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel7).addComponent(this.m_jBarcode1, -2, -1, -2)).addGap(14, 14, 14).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.search).addComponent(this.m_jSearch, -2, -1, -2))).addComponent(this.Enter2)).addContainerGap(14, 32767)));
    getAccessibleContext().setAccessibleName("By product");
  }

  private void m_jReference1ActionPerformed(ActionEvent paramActionEvent)
  {
    assignProductByReference();
  }

  private void searchActionPerformed(ActionEvent paramActionEvent)
  {
    assignProduct(JProductFinder.showMessage(this, this.m_dlSales, 1));
  }

  private void Enter2ActionPerformed(ActionEvent paramActionEvent)
  {
    assignProductByCode();
  }

  private void Enter1ActionPerformed(ActionEvent paramActionEvent)
  {
    assignProductByReference();
  }

  private void m_jBarcode1ActionPerformed(ActionEvent paramActionEvent)
  {
    assignProductByCode();
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.panels.AuxiliarFilter
 * JD-Core Version:    0.6.0
 */